<template>
    <div>
        <div>
            <div style="display: flex;justify-content: space-between">
                <div>
                    <el-input placeholder="请输入车辆品牌进行搜索，可以直接回车搜索..." prefix-icon="el-icon-search"
                              clearable
                              @clear="initBorrowInfo"
                              style="width: 350px;margin-right: 10px" v-model="keyword"
                              @keydown.enter.native="initBorrowInfo" :disabled="showAdvanceSearchView"></el-input>
                    <el-button icon="el-icon-search" type="primary" @click="initBorrowInfo" :disabled="showAdvanceSearchView">
                        搜索
                    </el-button>
                </div>
            </div>
        </div>
        <div style="margin-top: 10px">
            <el-table
                    :data="BorrowInfo"
                    stripe
                    border
                    v-loading="loading"
                    element-loading-text="正在加载..."
                    element-loading-spinner="el-icon-loading"
                    element-loading-background="rgba(0, 0, 0, 0.8)"
                    style="width: 100%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="id"
                        align="left"
                        fixed
                        label="车辆ID"
                        width="170">
                </el-table-column>
                <el-table-column
                        prop="model"
                        label="品牌型号"
                        fixed
                        align="left"
                        width="85">
                </el-table-column>
                <el-table-column
                        prop="mileage"
                        width="110"
                        label="总里程">
                </el-table-column>
                <el-table-column
                        prop="spend"
                        width="140"
                        label="总花销￥">
                </el-table-column>
                <el-table-column
                        prop="name"
                        width="140"
                        label="借车人">
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        width="160"
                        label="出借时间">
                </el-table-column>
                <el-table-column
                        prop="createBy"
                        width="180"
                        align="left"
                        label="出借操作人">
                </el-table-column>
                <el-table-column
                        prop="updateTime"
                        width="160"
                        label="归还时间">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        width="240"
                        label="操作">
                    <template slot-scope="scope">
                        <el-button style="padding: 3px" size="mini" type="primary" @click="back(scope.row)">
                            归还
                        </el-button>
                        <el-button style="padding: 3px" size="mini" type="primary" @click="mileAndSpend(scope.row)">
                            管理里程和花销
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div style="display: flex;justify-content: flex-end">
                <el-pagination
                        background
                        @current-change="currentChange"
                        @size-change="sizeChange"
                        layout="sizes, prev, pager, next, jumper, ->, total, slot"
                        :total="total">
                </el-pagination>
            </div>
        </div>
        <el-dialog
                :title="title"
                :visible.sync="dialogVisible3"
                width="20%">
            <div>
                <el-form :model="borrowCar" :rules="rules" ref="borrowForm">
                    <el-row>
                        <el-form-item label="请输入里程:">
                            <el-input type="number" size="mini" style="width: 120px" prefix-icon="el-icon-edit"
                                      v-model="borrowCar.mileage"
                                      placeholder="请输入里程"></el-input>
                            <span>公里</span>
                        </el-form-item>
                        <el-form-item label="请输入花销:">
                            <el-input type="number" size="mini" style="width: 120px" prefix-icon="el-icon-edit"
                                      v-model="borrowCar.spend"
                                      placeholder="请输入花销"></el-input>
                            <span>元</span>
                        </el-form-item>

                    </el-row>

                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click=" dialogVisible3= false">取 消</el-button>
                <el-button type="primary" @click="Change()">修改</el-button>
            </span>
        </el-dialog>
    </div>

</template>

<script>
    export default {
        name: "BorrowInfo",
        data() {
            return {
                searchValue: {

                    model: null,

                },
                title: '',
                borrowCar:{
                    cId:"",
                    mileage:0.00,
                    spend:0.00
                },
                importDataBtnIcon: 'el-icon-upload2',
                importDataDisabled: false,
                showAdvanceSearchView: false,
                allDeps: [],
                BorrowInfo: [],
                borrowCars: {
                    id:"",
                    uId:"",
                    cId:"",
                    name:"",
                    duration:0,
                    spend:0.00,
                    mileage:0.00
                },
                applyIns: {
                    id: "",
                    carId: "",
                    insId: "",
                    duration: 0,
                    basePrice: 0.00,
                    finalPrice: 0.00,
                },
                tip: "",
                loading: false,
                popVisible: false,
                popVisible2: false,
                dialogVisible: false,
                dialogVisible2: false,
                dialogVisible3: false,
                lookInses: [],
                total: 0,
                page: 1,
                keyword: '',
                size: 10,
                nations: [],
                joblevels: [],
                politicsstatus: [],
                positions: [],
                tiptopDegrees: ['本科', '大专', '硕士', '博士', '高中', '初中', '小学', '其他'],
                options: [],
                status: [{
                    name: "正常",
                    id: "正常"
                }, {
                    name: "不正常"
                    , id: "不正常"
                }],
                borrows: [{
                    name: "未借出",
                    id: 0
                }, {
                    name: "已借出"
                    , id: 1
                }],
                inputDepName: '所属部门',
                car: {
                    id: "",
                    model: "X5",
                    insstatu: "正常",
                    yearstatu: "正常",
                    mileage: 0,
                    borrow: "未借出",
                    spend: 0.00
                },
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                rules: {
                    model: [{required: true, message: '请输入车辆型号', trigger: 'blur'}],


                }
            }
        },
        mounted() {
            this.initBorrowInfo();
            // this.initData();
            // this.initPositions();
        },
        methods: {
            mileAndSpend(data){
                this.dialogVisible3=true;
                this.borrowCar=data;
            },
            searvhViewHandleNodeClick(data) {
                this.inputDepName = data.name;
                this.searchValue.departmentId = data.id;
                this.popVisible2 = !this.popVisible2
            },
            onError(err, file, fileList) {
                this.importDataBtnText = '导入数据';
                this.importDataBtnIcon = 'el-icon-upload2';
                this.importDataDisabled = false;
            },
            onSuccess(response, file, fileList) {
                this.importDataBtnText = '导入数据';
                this.importDataBtnIcon = 'el-icon-upload2';
                this.importDataDisabled = false;
                this.initEmps();
            },
            beforeUpload() {
                this.importDataBtnText = '正在导入';
                this.importDataBtnIcon = 'el-icon-loading';
                this.importDataDisabled = true;
            },
            exportData() {
                window.open('/employee/basic/export', '_parent');
            },
            emptyCar() {
                this.car = {
                    id: "",
                    brand: "",
                    model: "",
                    address: "",
                    telphone: null,
                    carmaster: "",
                    price: 0.00,
                    enginenum: "",
                }
            },
            showEditCarView(data) {
                // this.initPositions();
                this.title = '编辑车辆信息';
                this.car = data;
                this.dialogVisible = true;
            },
            Change() {
                this.borrowCar.cId = this.car.id;
                console.log("borrowInfo:", this.borrowCars);
                this.$refs['borrowForm'].validate(valid => {
                    if (valid) {
                        this.putRequest("/borrowInfo/basic/", this.borrowCar).then(resp => {
                            if (resp) {
                                this.dialogVisible3 = false;
                                this.initBorrowInfo();
                            }
                        })
                    }
                });
            },
            // borrowCar(data) {
            //     this.dialogVisible3 = true;
            //     this.applyIns = {
            //         id: "",
            //         cId: "",
            //         uId: "",
            //         duration: 0.00,
            //         mileage: 0.00,
            //         spend: 0.00
            //     };
            //     this.car = data;
            //     this.getRequest("/apply/userList/").then(resp => {
            //         if (resp) {
            //             this.options = resp;
            //         }
            //     })
            // },
            LookManager(data) {
                this.dialogVisible2 = true;
                this.DialogDataLoading(data.id);
            },
            DialogDataLoading(data) {
                this.getRequest("/lookIns/basic/" + data).then(resp => {
                    if (resp) {
                        this.lookInses = resp.data;
                    }
                })
            },
            back(data) {
                this.$confirm('请确定车已经正确入库', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/borrowInfo/basic/" ,data).then(resp => {
                        if (resp) {
                            this.initBorrowInfo();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            doAddCar() {
                if (this.car.id) {
                    this.$refs['carForm'].validate(valid => {
                        if (valid) {
                            this.putRequest("/car/basic/", this.car).then(resp => {
                                if (resp) {
                                    this.dialogVisible = false;
                                    this.initBorrowInfo();
                                }
                            })
                        }
                    });
                } else {
                    this.$refs['carForm'].validate(valid => {
                        if (valid) {
                            this.postRequest("/car/basic/", this.car).then(resp => {
                                if (resp) {
                                    this.dialogVisible = false;
                                    this.initBorrowInfo();
                                }
                            })
                        }
                    });
                }
            },
            handleNodeClick(data) {
                this.inputDepName = data.name;
                this.emp.departmentId = data.id;
                this.popVisible = !this.popVisible
            },
            showDepView() {
                this.popVisible = !this.popVisible
            },
            showDepView2() {
                this.popVisible2 = !this.popVisible2
            },
            getMaxWordID() {
                this.getRequest("/employee/basic/maxWorkID").then(resp => {
                    if (resp) {
                        this.emp.workID = resp.obj;
                    }
                })
            },
            initData() {
                if (!window.sessionStorage.getItem("nations")) {
                    this.getRequest('/car/basic/nations').then(resp => {
                        if (resp) {
                            this.nations = resp;
                            window.sessionStorage.setItem("nations", JSON.stringify(resp));
                        }
                    })
                } else {
                    this.nations = JSON.parse(window.sessionStorage.getItem("nations"));
                }
            },
            sizeChange(currentSize) {
                this.size = currentSize;
                this.initEmps();
            },
            currentChange(currentPage) {
                this.page = currentPage;
                this.initEmps();
            },
            showAddCarView() {
                this.emptyCar();
                this.title = '添加车辆';
                this.dialogVisible = true;
            },
            initBorrowInfo(type) {
                this.loading = true;
                let url = '/borrowInfo/basic/?page=' + this.page + '&size=' + this.size;

                url += "&model=" + this.keyword;

                this.getRequest(url).then(resp => {
                    this.loading = false;
                    if (resp) {
                        this.BorrowInfo = resp.data;
                        this.total = resp.total;
                    }
                });
            }
        }
    }
</script>

<style>
    /* 可以设置不同的进入和离开动画 */
    /* 设置持续时间和动画函数 */
    .slide-fade-enter-active {
        transition: all .8s ease;
    }

    .slide-fade-leave-active {
        transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
    }

    .slide-fade-enter, .slide-fade-leave-to
        /* .slide-fade-leave-active for below version 2.1.8 */
    {
        transform: translateX(10px);
        opacity: 0;
    }
</style>